import { defineStore } from 'pinia'
import { ref } from 'vue'
import router from "@/router/index.js";

export const useUserStore = defineStore(
    'user',
    () => {
      const token = ref('')
      const UserId = ref('')
      const UserName=ref('')
      const avatar = ref('')
      const nickname= ref('')


      const setToken = (newToken) => {
        token.value = newToken
      }

      const setUserId = (newUserId) => {
        UserId.value = newUserId
      }
      const setAvatar = (newAvatar) => {
        avatar.value = newAvatar
      }
      const setUserName = (newUserName) => {
        UserName.value = newUserName
      }
      const setNickname = (newNickname) => {
        nickname.value = newNickname
      }


      const removeUserId = () => {
        UserId.value = ''
      }

      const removeToken = () => {
        token.value = ''
      }
      const removeUserName = () => {
        UserName.value = ''
      }
      const removeAvatar = () => {
        avatar.value = ''
      }
      const removeNickname = () => {
        nickname.value = ''
      }
      // 退出登录时清除用户信息
      const logout = () => {
        removeToken()
        removeUserId()
        removeUserName()
          removeAvatar()
          removeNickname()

          router.push('/')
      }

      return {
        token,
        UserId,
        UserName,
          avatar,
          nickname,
          setNickname,
        setUserName,
        setToken,
        setUserId,
          setAvatar,
        removeToken,
        removeUserId,
          removeUserName,
          removeAvatar,
          removeNickname,
         logout,


      }
    },
    {
      persist: true
    }
)